﻿var SCA = SCA || {};
SCA.UI = SCA.UI || {};
SCA.UI.SiteSettings = SCA.UI.SiteSettings || {};

SCA.UI.SiteSettings.Dialog = function (siteSettingsTable) {
  

    this.siteSettingsTable = siteSettingsTable;
    
    this.Setup = function () {
        jQuery("#NewSiteSettingButton").button().click(function () {
            jQuery("#SiteSettingEditDialog").dialog({ title: "New Site Setting", width: "400px" });
        });

        jQuery("#SiteSettingEditDialog").on("dialogclose", function (event, ui) { jQuery(".validity-modal-msg").hide(); });
    }


    this.New = function () {
        //TODO: add SiteSettingType
        GetHiddenInputIndex().val("-1");
        GetInputUrl().val("");
        GetCheckBoxDefaultCredentials().attr("checked", false);
        GetInputUsername().val("");
        GetInputPassword().val("");
        GetCheckBoxClaimAuthentication().attr("checked", false);
    }


    this.Load = function (index) {

        if (SCA.AppConfiguration.SiteSettings == null)
            return;

        var siteSetting = GetSiteSetting(index);

        GetRadioButtonType(siteSetting).attr("checked", true);

        GetInputUrl().val(siteSetting.Url);

        GetCheckBoxDefaultCredentials().attr("checked", siteSetting.UseDefaultCredential);
        
        GetInputUsername().val(siteSetting.Username);

        GetInputPassword().val(siteSetting.Password);

        GetCheckBoxClaimAuthentication().attr("checked", siteSetting.UseClaimAuthentication);

        GetHiddenInputIndex().val(index);
    }

    this.Validate = function () {

        jQuery.validity.setup({ outputMode: "modal" });

        jQuery.validity.start();
        GetInputUrl().require("Required.");

        if (!GetCheckBoxDefaultCredentials().is(':checked')) {
            GetInputUsername().require("Required.");
            GetInputPassword().require("Required.");
        }

        var result = jQuery.validity.end();

        return result.valid;
    }


    this.Close = function()
    {
        jQuery("#SiteSettingEditDialog").dialog("close");
    }

    this.Save = function () {

        var isValid = this.Validate();
        if (!isValid)
            return;

        var index = GetHiddenInputIndex().val();

        var isNewItem = false;
        if (index == -1) //it's a new site setting
        {
            index = SCA.AppConfiguration.SiteSettings.push({}) - 1;
            isNewItem = true;
        }

        var siteSetting = GetSiteSetting(index);
        siteSetting.SiteSettingType = "SharePoint";
        siteSetting.Url = GetInputUrl().val();
        siteSetting.UseDefaultCredential = GetCheckBoxDefaultCredentials().is(':checked');
        siteSetting.Username = GetInputUsername().val();
        siteSetting.Password = GetInputPassword().val();
        siteSetting.UseClaimAuthentication = GetCheckBoxClaimAuthentication().is(':checked');

        if (isNewItem) {
            siteSetting.ID = SP.Guid.newGuid().toString();
            this.siteSettingsTable.AddRow(index);
        }
        else {
            this.siteSettingsTable.RefreshRow(index);
        }

        this.Close();
    }

    var GetSiteSetting = function (index) {
        return SCA.AppConfiguration.SiteSettings[index];
    }


    var GetRadioButtonType = function (siteSetting) {
        var radioButtonTypeId = "#RadioButtonSiteSettingsDialogType" + siteSetting.SiteSettingType;
        var radioButtonType = jQuery(radioButtonTypeId);
        return radioButtonType;
    }

    var GetInputUrl = function () {
        //TODO: could use caching for these
        return jQuery("#TextBoxSiteSettingsDialogUrl");
    }

    var GetCheckBoxDefaultCredentials = function () {
        return jQuery("#CheckBoxSiteSettingsDialogDefaultCredentials");
    }

    var GetInputUsername = function () {
        return jQuery("#TextBoxSiteSettingsDialogUsername");
    }

    var GetInputPassword = function () {
        return jQuery("#TextBoxSiteSettingsDialogPassword");
    }

    var GetCheckBoxClaimAuthentication = function () {
        return jQuery("#CheckBoxSiteSettingsDialogClaimAuthentication");
    }

    var GetHiddenInputIndex = function () {
        return jQuery("#HiddenFieldSiteSettingsDialogCurrentIndex");
    }

    this.Setup();
};